<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
use Phpben\Imi\Validate\Annotation\Validate;
// use ImiApp\ApiServer\Common\AuthApi;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Util\Http\Response;
use Imi\Util\Stream\MemoryStream;

use Imi\Server\View\Annotation\HtmlView;
use Imi\Server\View\Annotation\View;


/**
 * 

 * @OA\Tag(
 *   name="api",
 *   description="文档接口",
 *   @OA\ExternalDocumentation(
 *     description="查看更多",
 *     url="http://swagger.io"
 *   )
 * )
 * @Auth(name="token",nologin={"doc","html"})
 * @Controller("/api/")
 */
class Api extends Response
{
    /**
     * @Action
     * 
     * @return void
     */
    public function doc()
    {
        var_dump('doc');
        $html = file_get_contents(__DIR__.'/../../../api.json');
        $data = json_decode($html,true);

        /** @var \Imi\Server\Http\Message\Contract\IHttpResponse $response */
        $response = \Imi\RequestContext::get('response');
        
        $response->setStatus(200)
            ->setHeader('Access-Control-Allow-Origin', "*")
            ->withBody(new MemoryStream(json_encode($data,JSON_UNESCAPED_UNICODE)))
            ->send($html);
    }

    /**
    * html接口文档
    * @Action
    * 
    * @View(renderType="html")
    * @HtmlView(template="html")
    */
    public function html()
    {
        $path = dirname(__DIR__).'../../../public/tpmeapi.html';
        $html = file_get_contents($path);
        /** @var \Imi\Server\Http\Message\Contract\IHttpResponse $response */
        $response = \Imi\RequestContext::get('response');
        $response->getBody()->write($html);
        return $response;
    }
}
